package com.tianqu.tms.www.cardect.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.DectCodes;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.www.cardect.action.CarWDIpadTopListAction;
import com.tianqu.tms.www.cardect.viewhelper.CarWDIpadTopListViewHelper;

/**
 * 待检测（IPAD）列表页controller
 * 
 * @author tianshifu
 * 
 */
public class CarWDIpadTopListController extends TmsController {

	public static final String PAGING = "paging";
	public final static String SOURSE = "sourse";

	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			TmsDetectInfo car = TmsDetectInfo.getInstance();
			if (uc.getAttribute(CarWDIpadTopListAction.PAGENO) != null) {
				car.setPageNo(Integer.parseInt((String) uc
						.getAttribute(CarWDIpadTopListAction.PAGENO)));
			}
			if (uc.getAttribute(CarWDIpadTopListAction.MAXINDEX) != null) {
				car.setMaxIndex(Integer.parseInt((String) uc
						.getAttribute(CarWDIpadTopListAction.MAXINDEX)));
			}
			if (uc.getAttribute(CarWDIpadTopListAction.SORTKEY) != null) {
				car.setSortKey(Integer.parseInt((String) uc
						.getAttribute(CarWDIpadTopListAction.SORTKEY)));
			}
			if (uc.getAttribute(CarWDIpadTopListAction.ASC) != null) {
				car.setAsc((new Boolean((String) uc
						.getAttribute(CarWDIpadTopListAction.ASC)))
						.booleanValue());
			}
			// 业务处理
			int status = 0;
			String source = (String) uc.getAttribute(SOURSE);
			if (source.equals("服务器端")) {
				status = 2;
			}
			if (source.equals("客户端")) {
				status = 4;
			}
			// 通过车辆状态来查询车辆信息 返回list
			List<TmsDetectInfo> list = car.findAllWDIpadCarInfoByStatus(status);

			List<CarWDIpadTopListViewHelper> viewList = new ArrayList<CarWDIpadTopListViewHelper>();
			CarWDIpadTopListViewHelper view = new CarWDIpadTopListViewHelper();
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					TmsDetectInfo entity = (TmsDetectInfo) list.get(i);
					// 将entity数据转型放到viewhelp中
					view = new CarWDIpadTopListViewHelper();
					view = setViewFromEntity(entity, view);
					viewList.add(view);
				}
			}
			view.setWaitDetectIpadList(viewList);

			uc.setAttribute(TmsConstants.VIEWHELP, view);
			uc.setAttribute(PAGING, car);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 重构下list 将数据库数据转化成页面需要的数据
	 * 
	 * @throws TmsDaoException
	 */
	private CarWDIpadTopListViewHelper setViewFromEntity(TmsDetectInfo entity,
			CarWDIpadTopListViewHelper view) throws TmsDaoException {
		view.setCarSerial(entity.getCarSerial());
		view.setCarName(entity.getCarName());
		view.setCarLicense(entity.getCarLicense());
		view.setRegDate(entity.getRegDate());
		view.setCarCheckInsur(entity.getCarCheckInsur());
		view.setTrafficInsur(entity.getTrafficInsur());

		Codes tmsCode = Codes.getInstance();
		view.setColor(tmsCode.getItemValueByItemIdAndItemCode(
				Codes.VEHICLE_COLOR, Integer.parseInt(entity.getColor())));

		view.setUpdateTime(entity.getUpdateTime());
		view.setExhaust(StringUtil.getNewExhaust(entity.getExhaust()));// 毫升转成升+“L”
		DectCodes code = DectCodes.getInstance();

		view.setGearbox(code.getItemValueByItemIdAndItemCode(
				DectCodes.GEARBOX_TYPE, String.valueOf(entity.getGearbox())));
		view.setFuelDict(code.getItemValueByItemIdAndItemCode(
				DectCodes.FUEL_TYPE, String.valueOf(entity.getFuelDict())));
		view.setNature(code.getItemValueByItemIdAndItemCode(
				DectCodes.NATURE_TYPE, String.valueOf(entity.getNature())));
		view.setSource(code.getItemValueByItemIdAndItemCode(
				DectCodes.CAR_SOURSE, String.valueOf(entity.getSource())));
		// private String gearbox;
		// private String fuelDict;
		// private String nature;
		view.setCssFlag(entity.getSeeBsflag());

		return view;
	}
}
